package com.Home
{
	import com.*;
	import com.Engine.*;
	import com.Packet.*;
	import com.UI.*;
	
	import gs.*;
	import gs.easing.*;
	import gs.events.TweenEvent;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.dnd.*;
	import org.aswing.event.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.tree.AbstractLayoutCache;
	
	public class FrameHomeBuilding extends JFrame
	{
		//members define
		protected var panelBHImage:JPanel;
		protected var labelLevel:JLabel;
		protected var btn_levelup:JButton;
		protected var btn_accelerate:JButton;
		protected var btn_cancel:JButton;
		protected var labelInfo:JLabel;
		protected var labelBHDesc:MultilineLabel;
		protected var panelGuardImage:JPanel;
		protected var btn_dispatch:JButton;
		protected var btn_recall:JButton;
		protected var labelGuardDesc:MultilineLabel;
		protected var panelButtons:JPanel;
		
		private var btn_close:JButton;
		public function FrameHomeBuilding()
		{
			setResizable(false);
			setBorder(new EmptyBorder());
			
			btn_close = new JButton();
			btn_close.setLocation(new IntPoint(198, 0));
			btn_close.setSize(new IntDimension(27, 27));
			btn_close.setPreferredSize(new IntDimension(27, 27));
			this.getTitleBar().setCloseButton(btn_close);
			this.setClipMasked(false);
			getContentPane().setClipMasked(false);
			getTitleBar().getFrame().setClipMasked(false);
			btn_close.setClipMasked(false);
			getTitleBar().setMinimizeHeight(32);
			
			//component creation
			setSize(new IntDimension(368, 232));
			setPreferredSize(new IntDimension(368, 232));
			var layout0:EmptyLayout = new EmptyLayout();
			getContentPane().setLayout(layout0);
			
			panelBHImage = new JPanel();
			panelBHImage.setLocation(new IntPoint(10, 23));
			panelBHImage.setSize(new IntDimension(87, 57));
			panelBHImage.setPreferredSize(new IntDimension(87, 57));
			var layout1:EmptyLayout = new EmptyLayout();
			panelBHImage.setLayout(layout1);
			
			labelLevel = new JLabel();
			labelLevel.setLocation(new IntPoint(10, 7));
			labelLevel.setSize(new IntDimension(87, 16));
			labelLevel.setPreferredSize(new IntDimension(87, 16));
			labelLevel.setText("label");
			labelLevel.setHorizontalAlignment(AsWingConstants.CENTER);
			
			btn_levelup = new JButton();
			btn_levelup.setLocation(new IntPoint(109, 15));
			btn_levelup.setSize(new IntDimension(49, 23));
			btn_levelup.setPreferredSize(new IntDimension(49, 23));
			btn_levelup.setText("升级");
			
			btn_accelerate = new JButton();
			btn_accelerate.setLocation(new IntPoint(109, 15));
			btn_accelerate.setSize(new IntDimension(49, 23));
			btn_accelerate.setPreferredSize(new IntDimension(49, 23));
			btn_accelerate.setText("加速");
			
			btn_cancel = new JButton();
			btn_cancel.setLocation(new IntPoint(109, 52));
			btn_cancel.setSize(new IntDimension(49, 23));
			btn_cancel.setPreferredSize(new IntDimension(49, 23));
			btn_cancel.setText("取消");
			
			labelInfo = new JLabel();
			labelInfo.setLocation(new IntPoint(172, 66));
			labelInfo.setSize(new IntDimension(187, 16));
			labelInfo.setPreferredSize(new IntDimension(187, 16));
			labelInfo.setText("label");
			
			labelBHDesc = new MultilineLabel();
			labelBHDesc.setLocation(new IntPoint(172, 5));
			labelBHDesc.setSize(new IntDimension(187, 62));
			labelBHDesc.setPreferredSize(new IntDimension(187, 62));
			labelBHDesc.setWordWrap(true);
			
			panelGuardImage = new JPanel();
			panelGuardImage.setLocation(new IntPoint(18, 89));
			panelGuardImage.setSize(new IntDimension(70, 70));
			panelGuardImage.setPreferredSize(new IntDimension(70, 70));
			
			btn_dispatch = new JButton();
			btn_dispatch.setLocation(new IntPoint(109, 96));
			btn_dispatch.setSize(new IntDimension(49, 23));
			btn_dispatch.setPreferredSize(new IntDimension(49, 23));
			btn_dispatch.setText("派遣");
			
			btn_recall = new JButton();
			btn_recall.setLocation(new IntPoint(109, 133));
			btn_recall.setSize(new IntDimension(49, 23));
			btn_recall.setPreferredSize(new IntDimension(49, 23));
			btn_recall.setText("召回");
			
			labelGuardDesc = new MultilineLabel();
			labelGuardDesc.setLocation(new IntPoint(172, 87));
			labelGuardDesc.setSize(new IntDimension(187, 77));
			labelGuardDesc.setPreferredSize(new IntDimension(187, 77));
			labelGuardDesc.setWordWrap(true);
			
			panelButtons = new JPanel();
			panelButtons.setLocation(new IntPoint(9, 164));
			panelButtons.setSize(new IntDimension(351, 29));
			panelButtons.setPreferredSize(new IntDimension(351, 29));
			var layout2:FlowWrapLayout = new FlowWrapLayout();
			layout2.setPreferWidth(200);
			layout2.setAlignment(AsWingConstants.CENTER);
			layout2.setHgap(10);
			layout2.setVgap(3);
			layout2.setMargin(true);
			panelButtons.setLayout(layout2);
			
			//component layoution
			getContentPane().append(panelBHImage);
			getContentPane().append(labelLevel);
			getContentPane().append(btn_levelup);
			getContentPane().append(btn_accelerate);
			getContentPane().append(btn_cancel);
			getContentPane().append(labelInfo);
			getContentPane().append(labelBHDesc);
			getContentPane().append(panelGuardImage);
			getContentPane().append(btn_dispatch);
			getContentPane().append(btn_recall);
			getContentPane().append(labelGuardDesc);
			getContentPane().append(panelButtons);
			
			
			setImages();
			
			addEventListener(AWEvent.SHOWN,onFrameShown);
			addEventListener(FrameEvent.FRAME_CLOSING,onFrameClosing);
			addEventListener(AWEvent.HIDDEN,onFrameClosing);

		}
		private static function onFrameClosing(e:Event):void
		{
			if(lastShowingSecondFrame != null)
				lastShowingSecondFrame.hide();
			lastShowingSecondFrame = null;
			lastShowingFrame = null;
		}
		private static var lastShowingFrame:FrameHomeBuilding = null;
		private static function onFrameShown(e:Event):void
		{
			if(lastShowingFrame != null)
				lastShowingFrame.hide();
			lastShowingFrame = e.target as FrameHomeBuilding;
		}
		
		private static var lastShowingSecondFrame:JFrame = null;
		public static function onSecondFrameShown(e:Event):void
		{
			if(lastShowingSecondFrame != null)
				lastShowingSecondFrame.hide();
			lastShowingSecondFrame = e.target as JFrame;
			
			if(lastShowingFrame == null)
				return;
			if(lastShowingSecondFrame == null)
				return;
			
			var teamDestX:int = (Engine.W - (lastShowingSecondFrame.width+lastShowingFrame.width))/2;
			var teamDestY:int = (Engine.H - lastShowingFrame.height)/2;
			
			var listDestX:int = teamDestX + lastShowingFrame.width;
			var listDestY:int = teamDestY + (lastShowingFrame.height - lastShowingSecondFrame.height)/2;
			
			var tween:TweenLite = TweenLite.to(lastShowingFrame, 0.3, {x:teamDestX, y:teamDestY});
			var list_tween:TweenLite = TweenLite.to(lastShowingSecondFrame, 0.3, {x:listDestX, y:listDestY});
		}
		
		public static function onSecondFrameClosing(e:Event):void
		{
			lastShowingSecondFrame = null;
			if(lastShowingFrame == null)
				return;
			var teamDestX:int = (Engine.W - lastShowingFrame.width)/2;
			var teamDestY:int = (Engine.H - lastShowingFrame.height)/2;
			var tween:TweenLite = TweenLite.to(lastShowingFrame, 0.3, {x:teamDestX, y:teamDestY});
		}
		public function setImages():void
		{
			
			var img_char_background:DisplayObject = Client.getUIElement("img_home_bg") as DisplayObject
			setBackgroundDecorator(new AssetBackground(img_char_background));
			setSize(new IntDimension(img_char_background.width, img_char_background.height));
			setPreferredSize(new IntDimension(img_char_background.width, img_char_background.height));
			setSizeWH(img_char_background.width,img_char_background.height);
			setLocationXY((Engine.W - img_char_background.width) / 2,(Engine.H - img_char_background.height)/2);
			
			panelBHImage.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_default_image") as DisplayObject));
			//panelGuardImage.setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_default_image") as DisplayObject));
			panelGuardImage.setBackgroundDecorator(null);
			
			btn_levelup.wrapSimpleButton(Client.getUIElement("btn_2") as SimpleButton);
			btn_accelerate.wrapSimpleButton(Client.getUIElement("btn_2") as SimpleButton);
			btn_cancel.wrapSimpleButton(Client.getUIElement("btn_2") as SimpleButton);
			btn_dispatch.wrapSimpleButton(Client.getUIElement("btn_2") as SimpleButton);
			btn_recall.wrapSimpleButton(Client.getUIElement("btn_2") as SimpleButton);
			btn_levelup.setBackground(ASColor.GRAY);
			btn_accelerate.setBackground(ASColor.GRAY);
			btn_cancel.setBackground(ASColor.GRAY);
			btn_dispatch.setBackground(ASColor.GRAY);
			btn_recall.setBackground(ASColor.GRAY);

			
			//btn_accelerate.setVisible(false);
			btn_close.wrapSimpleButton(Client.getUIElement("btn_close") as SimpleButton);
			

			var filter:GlowFilter;
			var font:ASFont = new ASFont("宋体", 14, false, false, false, false);
			filter = new GlowFilter(0x000000, 1, 2, 2, 5);
			btn_levelup.setFont(font);
			btn_levelup.setForeground(ASColor.WHITE);
			btn_levelup.setTextFilters(new Array(filter));
			
			btn_accelerate.setFont(font);
			btn_accelerate.setForeground(ASColor.WHITE);
			btn_accelerate.setTextFilters(new Array(filter));
			
			btn_cancel.setFont(font);
			btn_cancel.setForeground(ASColor.WHITE);
			btn_cancel.setTextFilters(new Array(filter));
			
			btn_dispatch.setFont(font);
			btn_dispatch.setForeground(ASColor.WHITE);
			btn_dispatch.setTextFilters(new Array(filter));
			
			btn_recall.setFont(font);
			btn_recall.setForeground(ASColor.WHITE);
			btn_recall.setTextFilters(new Array(filter));

			getContentPane().setClipMasked(false);
			setClipMasked(false);
			panelGuardImage.setClipMasked(false);
			
			font = new ASFont("宋体", 12, false, false, false, false);
			labelBHDesc.setForeground(ASColor.WHITE);
			labelBHDesc.setText("    你可以派遣守卫来保护你的幻境建筑,守卫在别人掠夺或占领时会与你一同反抗入侵者.");
			labelBHDesc.setFont(font);
			
			labelInfo.setForeground(ASColor.GREEN);
			labelInfo.setText("升级剩余时间 00:00:15");
			labelInfo.setFont(font);
			
			labelGuardDesc.setForeground(ASColor.WHITE);
			labelGuardDesc.setText("    你可以派遣守卫来保护你的幻境建筑,守卫在别人掠夺或占领时会与你一同反抗入侵者.");
			labelGuardDesc.setFont(font);
			
			labelLevel.setForeground(ASColor.ORANGE);
			labelLevel.setFont(new ASFont("宋体", 13, false, false, false, false));
			labelLevel.setTextFilters(new Array(filter));
			
			//btn_dispatch.addActionListener(function():void{FrameHomeDispatch.show(objDest,_frame);});
			//btn_accelerate.addActionListener(function():void{FrameHomeAccelerate.show(this);});
			btn_levelup.addActionListener(onBtnLevelup);
			btn_cancel.addActionListener(onBtnCancel);
			btn_recall.addActionListener(onBtnRecall);
			
			
		}
		
		public var objDest:ObjectHomeBuilding = null;
		
		public function update(obj:ObjectHomeBuilding):void
		{
			if(obj == null)
				return;
			
			panelBHImage.setBackgroundDecorator(new AssetBackground(obj.imageBitmap));
			panelBHImage.pack();
			panelBHImage.paintImmediately();

			labelLevel.setText(obj.getLevel()+'级');
			objDest = obj;
			if(objDest.userObject == null)
				return;
			if(objDest.userObject.m_nState == Const.HOME_BUILDING_STATE_NORMAL)//objDest.userObject.m_nLevelupStartTime == null || objDest.userObject.m_nLevelupTime == null ||(objDest.userObject.m_nLevelupStartTime == 0 && objDest.userObject.m_nLevelupTime == 0))
			{
				btn_accelerate.setVisible(false);
				btn_levelup.setVisible(true);
				btn_levelup.setEnabled(true);
				btn_cancel.setEnabled(false);
				
				if(objDest.userObject.m_nLevel == 0)
					labelInfo.setText("");
				else
				{
					//labelInfo.setForeground(ASColor.RED);
					labelInfo.setText('升级需要:'+objDest.userObject.m_nLevelupReqairWood+'/'+CharInfo.wood+'仙木,'+objDest.userObject.m_nLevelupReqairFood+'/'+CharInfo.food+'仙粮');
					if(objDest.userObject.m_nLevelupReqairWood > CharInfo.wood || objDest.userObject.m_nLevelupReqairFood > CharInfo.food)
						labelInfo.setForeground(ASColor.RED);
					else
						labelInfo.setForeground(ASColor.GREEN);
				}
			}
			else
			{
				btn_accelerate.setVisible(true);
				btn_accelerate.setEnabled(true);
				btn_levelup.setVisible(false);
				btn_cancel.setEnabled(true);
				labelInfo.setForeground(ASColor.GREEN);
				labelInfo.setText("剩余升级时间: "+PanelMainHelp.formatTime(objDest.userObject.m_nLevelupTime-objDest.userObject.m_nLevelupStartTime));
			}
			var type:int = objDest.buildingHomeType;
			if(objDest.buildingHomeType == 0)
				type = objDest.userObject.m_nCSVId;
			var objCSV:Object = Client.getInstance().GetHomeBuildingCSV(type);
			if(objCSV != null)
			{
				labelBHDesc.setText('    '+objCSV.description);
			}
			if(objDest.userObject.m_nGuardType == null || objDest.userObject.m_nGuardType == PanelMain.PARTNER_TYPE_NULL)
			{
				btn_dispatch.setEnabled(true);
				btn_recall.setEnabled(false);
			}
			else
			{
				btn_dispatch.setEnabled(false);
				btn_recall.setEnabled(true);
			}
			if(objDest.userObject.m_nGuardType == PanelMain.PARTNER_TYPE_NULL)
			{
				if(_mcPet != null && panelGuardImage.contains(_mcPet))
				{
					panelGuardImage.removeChild(_mcPet);
					_mcPet = null;
				}
				if(_mcLoverImage != null && panelGuardImage.contains(_mcLoverImage))
				{
					panelGuardImage.removeChild(_mcLoverImage);
					_mcLoverImage = null;
				}
				if(_mcSlaveImage != null && panelGuardImage.contains(_mcSlaveImage))
				{
					panelGuardImage.removeChild(_mcSlaveImage);
					_mcSlaveImage = null;
				}
				if(_loaderLover != null)
				{
					_loaderLover.clear();
					_loaderLover = null;
				}
				if(_loaderSlave != null)
				{
					_loaderSlave.clear();
					_loaderSlave = null;
				}
			}
			if(objDest.userObject.m_nGuardType == PanelMain.PARTNER_TYPE_PET)
			{
				//objDest.userObject.m_nPetImageId;
				LoaderManager.getInstance().load(Engine.assetMapMonsterPath + "npc_"+ objDest.userObject.m_nPetImageId + '.swf',onLoaded);
			}
			
			if(objDest.userObject.m_nGuardType == PanelMain.PARTNER_TYPE_LOVER)
			{
				objDest.userObject.m_arLoverEquips;
				
				if(_loaderLover == null)
					_loaderLover = new CharImageLoader;
				
				if(_mcLoverImage != null && panelGuardImage.contains(_mcLoverImage))
				{
					panelGuardImage.removeChild(_mcLoverImage)
				}
				
				if(objDest.userObject.m_nLoverCharId == 0)
					return;

				if(_mcLoverImage != null && !panelGuardImage.contains(_mcLoverImage))
					panelGuardImage.addChildAt(_mcLoverImage,0);
				_loaderLover.loadChar(objDest.userObject.m_nLoverRace,objDest.userObject.m_nLoverGender,objDest.userObject.m_arLoverEquips,onLoverLoaded);
				
			}
			
			if(objDest.userObject.m_nGuardType == PanelMain.PARTNER_TYPE_SLAVE)
			{

				if(_loaderSlave == null)
					_loaderSlave = new CharImageLoader;
				if(_mcSlaveImage != null && panelGuardImage.contains(_mcSlaveImage))
				{
					panelGuardImage.removeChild(_mcSlaveImage)
				}
				
				if(objDest.userObject.m_nSlaveCharId == 0)
					return;
				if(_mcSlaveImage != null && !panelGuardImage.contains(_mcSlaveImage))
					panelGuardImage.addChildAt(_mcSlaveImage,0);
				_loaderSlave.loadChar(objDest.userObject.m_nSlaveRace,objDest.userObject.m_nSlaveGender,objDest.userObject.m_arSlaveEquips,onSlaveLoaded);
			}
			
			if(!Home.isInSelfHome())
			{
				btn_dispatch.setEnabled(false);
				btn_recall.setEnabled(false);
				btn_accelerate.setEnabled(false);
				btn_levelup.setEnabled(false);
				btn_cancel.setEnabled(false);
			}
		}

		private var _mcPet:MovieClip = null;
		private function onLoaded(e:Event):void
		{
			var l:LoaderInfo=e.target as LoaderInfo;
			l.removeEventListener(Event.COMPLETE,onLoaded);
			var loader:LoaderData = l.loader as LoaderData;
			if(loader != null)
			{
				var _appDomain:ApplicationDomain = loader.contentLoaderInfo.applicationDomain;
				if(_mcPet != null && panelGuardImage.containsChild(_mcPet))
				{
					panelGuardImage.removeChild(_mcPet)
					_mcPet = null;
				}
				_mcPet = Engine.getElement(_appDomain,"mc_object_attack") as MovieClip;
				if(_mcPet == null)
					return;
				_mcPet.cacheAsBitmap = true;
				_mcPet.scaleX = panelGuardImage.height/_mcPet.height;
				_mcPet.scaleY = panelGuardImage.height/_mcPet.height;
				_mcPet.x = (panelGuardImage.width - _mcPet.width)/2;
				_mcPet.y = 0;
				panelGuardImage.setBackgroundDecorator(null);
				panelGuardImage.addChildAt(_mcPet,0);
			}
		}
		
		private var _loaderLover:CharImageLoader = null;
		private var _mcLoverImage:MovieClip = null;
		private function onLoverLoaded():void
		{
			if(_mcLoverImage != null && panelGuardImage.contains(_mcLoverImage))
			{
				panelGuardImage.removeChild(_mcLoverImage)
				_mcLoverImage = null;
			}
			_mcLoverImage = _loaderLover.getImage("mc_object_attack",true);
			_mcLoverImage.cacheAsBitmap = true;
			_mcLoverImage.scaleX = panelGuardImage.height/_mcLoverImage.height;
			_mcLoverImage.scaleY = panelGuardImage.height/_mcLoverImage.height;
			_mcLoverImage.x = (panelGuardImage.width - _mcLoverImage.width)/2;
			_mcLoverImage.y = 0;
			panelGuardImage.setBackgroundDecorator(null);
			panelGuardImage.addChildAt(_mcLoverImage,0);
			
		}
		
		private var _loaderSlave:CharImageLoader = null;
		private var _mcSlaveImage:MovieClip = null;
		private function onSlaveLoaded():void
		{
			if(_mcSlaveImage != null && panelGuardImage.contains(_mcSlaveImage))
			{
				panelGuardImage.removeChild(_mcSlaveImage)
				_mcSlaveImage = null;
			}
			_mcSlaveImage = _loaderSlave.getImage("mc_object_attack",true);
			_mcSlaveImage.cacheAsBitmap = true;
			_mcSlaveImage.scaleX = panelGuardImage.height/_mcSlaveImage.height;
			_mcSlaveImage.scaleY = panelGuardImage.height/_mcSlaveImage.height;
			_mcSlaveImage.x = (panelGuardImage.width - _mcSlaveImage.width)/2;
			_mcSlaveImage.y = 0;
			panelGuardImage.setBackgroundDecorator(null);
			panelGuardImage.addChildAt(_mcSlaveImage,0);
		}
		public function onBtnLevelup(e:Event):void
		{
			Client.getInstance().sendPacketLogic(1302,[int(objDest.buildingHomeId),int(objDest.buildingHomeType),int(0)]);
		}
		public function onBtnCancel(e:Event):void
		{
			//Client.getInstance().sendPacketLogic(1302,[int(objDest.buildingHomeId),int(1)]);
			FrameInfo.showMessageDialog("","是否取消建筑 "+objDest.getName()+" 的升级?",handleOptionCancelLevelup,null,true,null,JOptionPane.YES|JOptionPane.NO);

		}
		public function onBtnRecall(e:Event):void
		{
			//Client.getInstance().sendPacketLogic(1302,[int(objDest.buildingHomeId),int(1)]);
			FrameInfo.showMessageDialog("","是否召回建筑 "+objDest.getName()+" 的守卫?",handleOptionCancelRecall,null,true,null,JOptionPane.YES|JOptionPane.NO);
			
		}
		
		private function handleOptionCancelRecall(button:uint):void
		{
			if(button == JOptionPane.YES)
			{
				//Client.getInstance().sendPacketLogic(16,[_currentPage,nIndexRemove]);
				Client.getInstance().sendPacketLogic(1303,[int(objDest.buildingHomeId),int(1)]);
			}
		}
		
		private function handleOptionCancelLevelup(button:uint):void
		{
			if(button == JOptionPane.YES)
			{
				//Client.getInstance().sendPacketLogic(16,[_currentPage,nIndexRemove]);
				Client.getInstance().sendPacketLogic(1302,[int(objDest.buildingHomeId),int(objDest.buildingHomeType),int(1)]);
			}
		}

		public static var _frame:FrameHomeBuilding = null;
		public static function show(obj:ObjectHomeBuilding):void
		{
			if(_frame == null)
			{
				_frame = new FrameHomeBuilding();
				PanelMainHelp.getInstance().addChild(_frame);
			}
			_frame.show();
			_frame.setLocationXY((Engine.W - _frame.width) / 2,(Engine.H - _frame.height)/2);
			_frame.update(obj);
			PanelMainHelp.getInstance().bringToTop(_frame);
			
		}
		public static function hide():void
		{
			if(isShowing())_frame.hide();
		}
		public static function isShowing():Boolean
		{
			return(_frame != null&& _frame.isShowing());
		}
	}
}